import sys

def getContent(line):
    return line.split('}')[0]
def getContent(str, line):
    return line.split(str)[1].split('}')[0]

reader = open('./bib/bib.bib', 'r')
writer = open('./new_bib.bib', 'w')

counter = 0
type = ''
author = ''
title = ''
confer = ''
year = ''
for line in reader:
    line = line.strip()
    if len(line) == 0:
        continue
    if line[0] == '@':
        counter += 1
        if line.lower().find("@inproceedings") >= 0:
            type = 'proceeding'
        elif line.lower().find("@inbook") >=0:
            type = 'book'
        elif line.lower().find("@article") >= 0:
            type = 'jour'   
    elif line.find('author = {') >=0:
        author = getContent('author = {', line)
    elif line.find('booktitle = {') >= 0:
        confer = getContent('booktitle = {', line)
    elif line.find('journal = {') >= 0:
        confer = getContent('journal = {', line)
    elif line.find('year = {') >= 0:
        year = getContent('year = {', line)
    elif line.find('title = {') >= 0:
        title = getContent('title = {', line)
    if line == '}':
        writer.write('\\bibitem')
        writer.write('{' + type + '} ')
        writer.write(author + '.: ')
        writer.write(title + '. ')
        writer.write(confer + ' ')
        writer.write('(' + year + ')')
        writer.write('\n')
reader.close()
writer.close()
